/***************************************************************************//**
 * @file 
 * @brief RAIL Test Application
 *******************************************************************************
 * # License
 * <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
 *******************************************************************************
 *
 * SPDX-License-Identifier: Zlib
 *
 * The licensor of this software is Silicon Laboratories Inc.
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 ******************************************************************************/

// -----------------------------------------------------------------------------
//                                   Includes
// -----------------------------------------------------------------------------
#include "sl_component_catalog.h"
#include "sl_system_init.h"
#include "sl_system_kernel.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "main.h"
#include "app_task.h"
#include "common.h"
#include "rf_process.h"
#include "rail_types.h"
#include "define.h"
#include "app_flash.h"
#include "app_param_store.h"
#include "bsp_timer.h"
#include "app_reserve_day.h"
#include "port_ctrl.h"
// -----------------------------------------------------------------------------
//                              Macros and Typedefs
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
//                          Static Function Declarations
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
//                                Global Variables
// -----------------------------------------------------------------------------
/************************************************
Semaphore
*************************************************/

DataFrame Data = {0};
OTAParaFame ota = {0};

RAIL_Handle_t railHandle;
// -----------------------------------------------------------------------------
//                                Static Variables
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
//                          Public Function Definitions
// -----------------------------------------------------------------------------

void SysInit(void)
{
    // Initialize Silicon Labs device, system, service(s) and protocol stack(s).
    // Note that if the kernel is present, processing task(s) will be created by
    // this call.
    /* Set new vector table */
    //SCB->VTOR = (uint32_t)FLASH_APP0_ADDR;

    //  NVIC_SetPriority(TIMER1_IRQn, 6);

    #if configUSE_WDOG
    bsp_WDOGInit();
    #endif

    railHandle = rail_init();
    RAIL_SetTxPower(railHandle, 70); //上电默认配置为最大19dBm,10dBm/10.02dBm

    //  RAIL_StartTxStream(railHandle, 0, RAIL_STREAM_CARRIER_WAVE);
    //  while(1);
    //检查参数区消息是否有效
    store_msg_t* message;
    message = store_msg_init();
    if(message)
    {
        //恢复记录
        set_port_opt(message->port);
        day_tick_clear();
        set_reserved_days(message->time_remain);
    }
    else
    {
        //恢复记录失败,设置默认值
        set_port_opt(PORT_OPERATION_DEFAULT);
        day_tick_clear();
        set_reserved_days(TIME_REMAIN_FOREVERY);
    }
    WriteReadPartNum();               //可以修改partnum
    Timer1Init();
    //
    //  OTAAllParameterGet();             //读写OTA的参数
    //
    //  INTX_ENABLE();                    //打开所有中断
}


TaskHandle_t  rf_send_task_handler;
QueueHandle_t radio_operation_queue = NULL;
QueueHandle_t flash_operation_queue = NULL;

void AppTaskCreate(void)
{
/*
* OS Task Creat
*/
    xTaskCreate(vRadioOperationTask,
            "radio_oper",
            RADIO_OPERATION_TASK_STACK_SIZE,
            NULL,
            RADIO_OPERATION_TASK_PRIORITY,
            &rf_send_task_handler);

    xTaskCreate(vFlashOperationTask,
            "flash_oper",
            FLASH_OPERATION_TASK_STACK_SIZE,
            NULL,
            FLASH_OPERATION_TASK_PRIORITY,
            NULL);

    radio_operation_queue = xQueueCreate( 10, sizeof(radio_operation_t));

    flash_operation_queue = xQueueCreate( 5, sizeof(flash_operation_t));
}


void vApplicationMallocFailedHook(void)
{
//    tASSERT(0, ASSERT_LEVEL_HIGH);
    while(1);
}

void vApplicationDaemonTaskStartupHook (void)
{
//    tASSERT(0, ASSERT_LEVEL_HIGH);
    while(1);
}

void vApplicationStackOverflowHook (TaskHandle_t xTask, char *pcTaskName)
{
    (void)    xTask;
    (void)    pcTaskName;
//    DEBUG(pcTaskName, false);
//    tASSERT(0, ASSERT_LEVEL_HIGH);
    while(1);
}

void system_reset(void)
{
    __NVIC_SystemReset();
}

/******************************************************************************
 * Main function
 *****************************************************************************/
int main(void)
{
    /*硬件相关初始化*/
    sl_system_init();

    SysInit();
    // Initialize the application. For example, create periodic timer(s) or
    // task(s) if the kernel is present.
    elab_run();

    AppTaskCreate();

    elab_system_running();
    // Start the kernel. Task(s) created in app_init() will start running.
    sl_system_kernel_start();
    while(1);
}
